{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a17441da",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-15T07:27:30.097991Z",
     "start_time": "2022-12-15T07:27:28.203538Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2.12343079, 1.14473864],\n",
       "       [3.0710525 , 1.46099371],\n",
       "       [2.21689683, 1.34773765],\n",
       "       [2.19737542, 1.39898418],\n",
       "       [2.53642585, 1.18443323]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Y=XW+B\n",
    "w = np.random.rand(3,2)\n",
    "x = np.random.rand(5,3)\n",
    "b = np.random.normal(0, 1, (1, 2))\n",
    "y = np.dot(x,w) + b\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b3672a4a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-15T07:27:30.119908Z",
     "start_time": "2022-12-15T07:27:30.107938Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1.],\n",
       "       [1., 1.],\n",
       "       [1., 1.],\n",
       "       [1., 1.],\n",
       "       [1., 1.]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dout = np.ones_like(y)\n",
    "dout"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "21e71e03",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-15T07:27:30.181741Z",
     "start_time": "2022-12-15T07:27:30.142845Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.1394752 , 0.90939674, 1.38569016],\n",
       "       [1.1394752 , 0.90939674, 1.38569016],\n",
       "       [1.1394752 , 0.90939674, 1.38569016],\n",
       "       [1.1394752 , 0.90939674, 1.38569016],\n",
       "       [1.1394752 , 0.90939674, 1.38569016]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵求导原则，x和dx的形状相同\n",
    "# 求x的导数dx\n",
    "dx = np.dot(dout,w.T)\n",
    "dx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a28f0391",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-15T07:27:30.225631Z",
     "start_time": "2022-12-15T07:27:30.193712Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.02514548, 0.01530996],\n",
       "       [0.02514548, 0.01530996],\n",
       "       [0.02514548, 0.01530996],\n",
       "       [0.02514548, 0.01530996],\n",
       "       [0.02514548, 0.01530996]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 验证，通过梯度下降法\n",
    "y - (np.dot((x-dx*0.01),w) + b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c2b20b88",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-15T07:27:30.260530Z",
     "start_time": "2022-12-15T07:27:30.238589Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2.47529096, 2.47529096],\n",
       "       [1.81021435, 1.81021435],\n",
       "       [2.20533244, 2.20533244]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求w的导数dw\n",
    "dw = np.dot(x.T,dout)\n",
    "dw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8744ab63",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-15T07:27:30.306407Z",
     "start_time": "2022-12-15T07:27:30.275491Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.00616494, 0.0041979 ],\n",
       "       [0.01564116, 0.00736045],\n",
       "       [0.0070996 , 0.00622789],\n",
       "       [0.00690439, 0.00674036],\n",
       "       [0.01029489, 0.00459485]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 验证，通过梯度下降法\n",
    "y - (np.dot(x,(w-0.01*w)) + b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "85877e45",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-15T07:27:30.370237Z",
     "start_time": "2022-12-15T07:27:30.320370Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5., 5.])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db = np.sum(dout,axis=0)\n",
    "db"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0d92bcb9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-15T07:27:30.410130Z",
     "start_time": "2022-12-15T07:27:30.375223Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.05, 0.05],\n",
       "       [0.05, 0.05],\n",
       "       [0.05, 0.05],\n",
       "       [0.05, 0.05],\n",
       "       [0.05, 0.05]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 验证，通过梯度下降法\n",
    "y - (np.dot(x,w) + b-0.01*db)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
